/**************************************************************************************
*  jcommon.h
*
*  Forward definitions of all global variables and functions.
*
**************************************************************************************
*
*  The contents of this file are subject to the RTJ COMPUTING non-commercial
*  source code license agreement ("License"); you may not use this file except in
*  compliance with the License. A copy of the License (license.terms) can be
*  found in the root of installation folder or on the web at
*  http://www.rtjcom.com/Files/license.terms
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
*  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
*  MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*  IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
*  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
*  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*  OTHER DEALINGS IN THE SOFTWARE.
*
*  Copyright (c) 2000-2002 RTJ Computing Pty. Ltd. All rights reserved.
*
**************************************************************************************/

#ifndef _GLOBALS_H
#define _GLOBALS_H

#include "jvm.h"

/*
** Global variables definitions.
*/
extern uint8       excep_count;
extern uint8       vm_excep;
extern uint8       vm_excep1;
extern uint8       vm_status;
extern uint8       thr_count;
extern uint8       thr_switch;
extern uint8       vm_args_count;

extern ref_t       *excep_refs[RUNTIME_EXCEPTIONS];
extern uint8       *vm_pc;
extern value_t     *vm_sp;
extern uint32       vm_time_ms;
extern ptrval       frame_size;
extern ptrval       inst_size;
extern vm_config_t *config;
extern classlock_t *class_locks;
extern thread_t    *ev_thread;
extern uint16      ev_index;
extern timer_t     *sw_timers;
extern uint8       *heap_ptr;
extern uint8       *frame_ptr;
extern value_t     *vm_args;
extern class_t    **clinit_list;
extern thread_t    *thr_active;
extern value_t     *static_start;
extern strings_t   *strings;
extern thread_t    *threads;
extern ref_t       *refs;
extern ref_t       *app_refs;
extern uint8       *objdata_start;
extern uint8       *bc_item;

extern const native_func_ptr sys_native_tbl[];
extern const bc_handler bytecode_table[];

#define VM_STATIC_END   ((uint8 *)strings)
#define VM_STRINGS_END  ((uint8 *)threads)
#define VM_THREADS_END  ((uint8 *)refs)
#define VM_REFS_END     ((uint8 *)objdata_start)

/*
** Function prototypes from J_GC
*/
#if ENABLE_GC
bool walkAll(void);
void compactHeapForced(void);
#endif

/*
** Function prototypes from J_VM
*/
void removeFromStrTable(ref_t *string);
ref_t *createObject(class_t *cl);
bool run(method_t *);
bool run_init(ref_t *ref, ref_t *strRef);
void fillInStackTrace(void);
int32 expandByte(int8 bval);
int32 expandShort(int16 sval);
void setupMethArgs(method_t *method);
ref_t *findInStrTable(uint16 hash);
ref_t *createStringObject(const_t *cons);
bool isSubclass(class_t *S, class_t *T);
bool handle_excep(void);
bool run_clinit(class_t *class_ptr);

/*
** Function prototypes from J_BCODE
*/
uint8 *getLocation(uint16 index);

/*
** Function prototypes from J_THREAD
*/
bool initThreads1(void);
bool initThreads2(void);
void switchThreads(void);
void deleteThread(thread_t *thread);
bool createThread(ref_t *ref);
thread_t *findThread(ref_t *ref);
void vmTimeSlice(void);
void acquireObjectLock(thread_t *thread, ref_t *ref);
void acquireClassLock(thread_t *thread);
bool releaseLocks(void);
bool releaseObjectLock(ref_t *ref);

/*
** Function prototypes from J_MEM
*/
void clearMem(uint8 *mem, int32 len);
ref_t *findFreeRef(void);
ref_t *allocHeapMem(ptrval size);
bool createFrame(method_t *method);
bool releaseFrame(value_t *retval);

/*
** Function prototypes from J_FLOAT
*/
#if ENABLE_FLOAT
#define NaN   0x7FC00000
float32 floatAdd(float32 a, float32 b);
float32 floatSub(float32 a, float32 b);
float32 floatMul(float32 a, float32 b);
float32 floatDiv(float32 a, float32 b);
float32 floatMod(float32 a, float32 b);
bool floatEQ(float32 a, float32 b);
bool floatNE(float32 a, float32 b);
bool floatGT(float32 a, float32 b);
bool isNaN(float32 value);
float32 floatNeg(float32 value);
float32 int2float(int32 value);
int32 float2int(float32 value);
bool stringToFloat(uint8 *buff, float32 *value);
int16 floatToString(float value, uint8 *buff, int16 length);
#endif

#if ENABLE_TEXT_NOTIFY
extern const char *const notify_msg[];
extern const char *const excep_msg[];
extern const char *const mem_excep_msg[];
extern const char *const int_excep_msg[];
#endif

#if ENABLE_STRING_EXCEPTIONS
extern const uint8 *const excep_str_table[];
#endif

#endif

